package com.edu.smsys.controller;

import com.edu.smsys.dao.entity.ClassEntity;
import com.edu.smsys.dao.entity.EnrolEntity;
import com.edu.smsys.model.vo.ClassVO;
import com.edu.smsys.service.impl.ClassService;
import com.edu.smsys.service.impl.EnrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级控制器
 */
@Controller
@RequestMapping("/classes")
public class ClassesController extends BaseController {
    @Autowired
    private ClassService classService;

    /**
     * 查询所有班级信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/classlist")
    public String classList(Model model) {
        //调用班级业务层读取班级信息
        List<ClassEntity> entities = classService.findAll();
        //返回给页面
        List<ClassVO> voList = classService.findAllVO();
        model.addAttribute("voList", voList);
        //TODO
        //定义到哪个视图中
        return VIEW_CONTENT + "class/classes";
    }

    /**
     * 删除班级信息
     *
     * @param id 要删除的id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        classService.deleteEntity(id);
        return "redirect:/classes/classlist";//转发到查询所有所有班级信息
    }

    /**
     * 修改班级信息
     *
     * @param classEntity
     * @return
     */
    @RequestMapping("/update")
    public String update(ClassEntity classEntity) {
        classService.updateEntity(classEntity);
        return "redirect:/classes/classlist";//转发到查询所有班级信息
    }

    /**
     * 添加班级信息页面显示
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        return VIEW_CONTENT + "class/addclass";
    }

    /**
     * 添加班级信息执行
     *
     * @param classEntity
     * @return
     */
    @RequestMapping("/adddo")
    public String addDo(ClassEntity classEntity) {
        classService.addEntity(classEntity);
        return "redirect:/classes/classlist";//转发到查询所有班级信息
    }

}
